package ru.amse.baltijsky.javascheme.frames.main;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChooseClassMethodDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JComboBox classList;
    private JComboBox methodList;
    private JLabel classListLabel;
    private JLabel methodListLabel;

    private String selectedClass = "";
    private String selectedMethod = "";
    private IMethodGetter methodGetter;
    private Frame mainFrame;
    private boolean accepted = false;
    private boolean exiting = false;
    private boolean loadingClasses = false;
    private boolean loadingMethods = false;

    public ChooseClassMethodDialog(Frame mainFrame) {
        super(mainFrame, "Choose class and method", true);

        this.mainFrame = mainFrame;
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                clearAndHide();
            }
        });
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clearAndHide();
                accepted = true;
            }
        });
        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                clearAndHide();
                accepted = false;
            }
        });
        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                classList.requestFocusInWindow();
            }
        });
        classList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!exiting && !loadingClasses) {
                    afterClassSelected();
                }
            }
        });
        methodList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (!exiting && !loadingMethods) {
                    selectedMethod = (String) methodList.getSelectedItem();
                }
            }
        });
        setResizable(false);
        setLocationByPlatform(true);
    }

    public void showDialog(String[] classStrings, IMethodGetter methodGetter) {
        selectedMethod = "";
        accepted = false;
        exiting = false;
        this.methodGetter = methodGetter;
        buttonOK.setEnabled(false);

        loadingClasses = true;
        for (String s : classStrings) {
            classList.addItem(s);
        }
        loadingClasses = false;
        afterClassSelected();

        setLocationRelativeTo(mainFrame);

        setResizable(true);
        pack();
        setResizable(false);

        setVisible(true);
    }

    private void afterClassSelected() {
        selectedClass = (String) classList.getSelectedItem();
        loadingMethods = true;
        methodList.removeAllItems();
        loadingMethods = false;
        methodList.setEnabled(true);
        for (String s : methodGetter.getMethodsByClass(selectedClass)) {
            methodList.addItem(s);
        }
        selectedMethod = (String) methodList.getSelectedItem();
        buttonOK.setEnabled(true);
    }

    public String getSelectedClass() {
        return selectedClass;
    }

    public String getSelectedMethod() {
        return selectedMethod;
    }

    public boolean isAccepted() {
        return accepted;
    }

    private void clearAndHide() {
        exiting = true;
        classList.removeAllItems();
        methodList.removeAllItems();
        setVisible(false);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1));
        contentPane.setVisible(true);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        panel1.add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_VERTICAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        buttonOK = new JButton();
        buttonOK.setText("OK");
        panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton();
        buttonCancel.setText("Cancel");
        panel2.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
        contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        classListLabel = new JLabel();
        classListLabel.setText("Select Class");
        classListLabel.setDisplayedMnemonic('C');
        classListLabel.setDisplayedMnemonicIndex(7);
        panel3.add(classListLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        methodListLabel = new JLabel();
        methodListLabel.setText("Select Method");
        methodListLabel.setDisplayedMnemonic('M');
        methodListLabel.setDisplayedMnemonicIndex(7);
        panel3.add(methodListLabel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        classList = new JComboBox();
        panel3.add(classList, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        methodList = new JComboBox();
        methodList.setEnabled(false);
        panel3.add(methodList, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
